import Api from '../api'
const state = {
	projectInfo: {}, // 大项目（前端这里叫大项目，但后端这里叫区项目，所以很多接口传递大项目id时用的是名称叫areaProjectId）
	areaProjectInfo: {}, // 小项目信息（前端这里叫区项目也叫小项目，后端这里接口传递小项目id用的是projectId和上面相反了）
	previewList: [], // 预览文件列表
	showPreview: false, // 是否展示文件列表
	activePreviewFile: {}, // 当前预览的文件信息
	filePath: '', // 当前预览的文件路径
	fileIndex: 0 // 当前预览的文件下标（位置）
}

const mutations = {
	SET_PROJECT: (state, projectInfo) => {
		state.projectInfo = projectInfo
	},
	SET_AREA_PROJECT: (state, projectInfo) => {
		state.areaProjectInfo = projectInfo
	},

	SET_PREVIEW_LIST: (state, previewList) => {
		state.previewList = previewList
	},
	SET_SHOW_PREVIEW: (state, showPreview) => {
		state.showPreview = showPreview
	},
	SET_ACTIVE_FILE: (state, file) => {
		state.activePreviewFile = file
	},
	SET_FILE_INDEX: (state, idx) => {
		state.fileIndex = idx
	},
	SET_FILE_PATH: (state, filePath) => {
		if (state.filePath !== filePath) {
			state.filePath = filePath
		}
	}
}

const actions = {
	getProjectInfo({ commit }, id) {
		return new Promise((resolve, reject) => {
			Api.getProjectInfo(id).then(res => {
				commit('SET_PROJECT', res)
				resolve()
			})
		})
	},
	getAreaProjectInfo({ commit }, data) {
		return new Promise((resolve, reject) => {
			Api.getAreaProjectInfo(data).then(res => {
				commit('SET_AREA_PROJECT', res)
				resolve()
			})
		})
	},
	// 设置点击列表附件个数进来的文件列表
	setPreviewList({ commit, state }, { list, projectId, index = 0, isArchive = false }) {
		if (!list.length) return
		if (state.showPreview) return
		const previewList = list.map(item => {
			return {
				...item,
				isArchive // 是否归档
			}
		})
		commit('SET_SHOW_PREVIEW', true)
		commit('SET_PREVIEW_LIST', previewList)
		commit('SET_ACTIVE_FILE', previewList[index])
		console.log('当前预览的文件:', previewList[index])
		commit('SET_FILE_PATH', previewList[index].filePath)
		commit('SET_FILE_INDEX', index)
		return new Promise(resolve => {
			const ids = list.map(item => item.id).join(',')
			Api.getFileIsArchive(ids).then(res => {
				const list = res.map(el => ({
					...el,
					projectId: projectId
				}))
				commit('SET_PREVIEW_LIST', list)
				commit('SET_ACTIVE_FILE', list[index])
				resolve()
			})
		})
	},
	// 当点击的是资料列表名称进来时
	setPreviewFolderIdList({ commit, state }, data) {
		if (state.showPreview) return
		commit('SET_SHOW_PREVIEW', true)
		return new Promise((resolve, reject) => {
			Api.page(
				{
					current: 1,
					size: -1,
					queryMode: 0,
					folderId: data.folderId,
					projectId: data.projectId
				},
				{
					cacheTime: 20000
				}
			).then(res => {
				const previewList = res.records.map(item => {
					return {
						...item,
						isArchive: true // 是否归档
					}
				})
				commit('SET_PREVIEW_LIST', previewList)
				const idx = res.records.findIndex(item => item.id === data.fileId)
				if (idx === -1) {
					commit('SET_ACTIVE_FILE', previewList[0])
					commit('SET_FILE_PATH', previewList[0].filePath)
					commit('SET_FILE_INDEX', 0)
				} else {
					commit('SET_ACTIVE_FILE', previewList[idx])
					commit('SET_FILE_PATH', previewList[idx].filePath)
					commit('SET_FILE_INDEX', idx)
				}

				resolve()
			})
		})
	},
	changeActiveFile({ commit, state }, idx) {
		const item = state.previewList[idx]
		commit('SET_ACTIVE_FILE', item)
		commit('SET_FILE_INDEX', idx)
		commit('SET_FILE_PATH', item.filePath)
	},
	setFileArchive({ commit, state }) {
		const newList = state.previewList
		newList[state.fileIndex].isArchive = true
		commit('SET_PREVIEW_LIST', newList)
		commit('SET_ACTIVE_FILE', newList[state.fileIndex])
	}
}
const getters = {
	getProjectInfo: state => {
		return state.projectInfo
	},
	projectId: state => {
		return state.projectInfo.id
	},
	projectNo: state => {
		return state.projectInfo.projectNo
	},
	getAreaProjectInfo: state => {
		return state.areaProjectInfo
	},
	areaProjectId: state => {
		return state.areaProjectInfo.id
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions,
	getters
}
